package net.sureon.web.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sureon.biz.CompanyBO;
import net.sureon.biz.CompanyMgr;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class ManageCustomer_bak extends MultiActionController {
	private String successView;
//	private UserMgr userMgr;
//	private GroupMgr groupMgr;
	private CompanyMgr companyMgr;

	/**
	 * @return the successView
	 */
	public String getSuccessView() {
		return successView;
	}

	/**
	 * @param successView
	 *            the successView to set
	 */
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	private static final String ATTRI_COMPANYS = "companys";
	private static final String PARAM_VIEW = "view";
//	private static final String PARAM_VAL_POTENTIAL = "potential";
	private static final String PARAM_VAL_APPLICATION_APPROVAL = "applicationApproval";
	private static final String PARAM_VAL_CUSTOMER_INFO = "cutomerInfo";

	public ModelAndView dispatchURL(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		List<CompanyBO> allCompanies = null;
		// 潜在客户
//		if (PARAM_VAL_POTENTIAL.equals(request.getParameter(PARAM_VIEW))) {
//			if (Action.WriteGroup.probeAccess(userMgr.getUser(AppContext.getCompanyID(), AppContext.getUserID()))) {
//				ModelAndView view = new ModelAndView(getSuccessView(), result);
//				users = new ArrayList<UserBO>();
//				List<Long> userIDs = userMgr.getChildUsersWithConfiguredLevel(AppContext.getCompanyID(), AppContext.getUserID());
//				for(Long userID : userIDs){
//					users.add(userMgr.getUser(AppContext.getCompanyID(), userID));
//				}
//			}
//			allCompanies = companyMgr.getAllCompanies();
//			result.put(ATTRI_COMPANYS, allCompanies);
//			result.put(PARAM_VIEW, request.getParameter(PARAM_VIEW));
//			ModelAndView view = new ModelAndView(getSuccessView(), result);
//			return view;
//		} 
		// 试用审批
		if (PARAM_VAL_APPLICATION_APPROVAL.equals(request.getParameter(PARAM_VIEW))) {
//			if (Action.WriteGroup.probeAccess(userMgr.getUser(AppContext.getCompanyID(), AppContext.getUserID()))) {
//				ModelAndView view = new ModelAndView(getSuccessView(), result);
//				users = new ArrayList<UserBO>();
//				List<Long> userIDs = userMgr.getChildUsersWithConfiguredLevel(AppContext.getCompanyID(), AppContext.getUserID());
//				for(Long userID : userIDs){
//					users.add(userMgr.getUser(AppContext.getCompanyID(), userID));
//				}
//			}
			allCompanies = companyMgr.getAllCompanies();
			result.put(ATTRI_COMPANYS, allCompanies);
			result.put(PARAM_VIEW, request.getParameter(PARAM_VIEW));
			ModelAndView view = new ModelAndView(getSuccessView(), result);
			return view;
		}
		// 客户信息
		if (PARAM_VAL_CUSTOMER_INFO.equals(request.getParameter(PARAM_VIEW))) {
//			if (Action.WriteGroup.probeAccess(userMgr.getUser(AppContext.getCompanyID(), AppContext.getUserID()))) {
//				ModelAndView view = new ModelAndView(getSuccessView(), result);
//				users = new ArrayList<UserBO>();
//				List<Long> userIDs = userMgr.getChildUsersWithConfiguredLevel(AppContext.getCompanyID(), AppContext.getUserID());
//				for(Long userID : userIDs){
//					users.add(userMgr.getUser(AppContext.getCompanyID(), userID));
//				}
//			}
			allCompanies = companyMgr.getAllCompanies();
			result.put(ATTRI_COMPANYS, allCompanies);
			result.put(PARAM_VIEW, request.getParameter(PARAM_VIEW));
			ModelAndView view = new ModelAndView(getSuccessView(), result);
			return view;
		} else {
			// 潜在客户
			allCompanies = companyMgr.getAllCompanies();
			result.put(ATTRI_COMPANYS, allCompanies);
			result.put(PARAM_VIEW, request.getParameter(PARAM_VIEW));
			ModelAndView view = new ModelAndView(getSuccessView(), result);
			return view;
		}
	}

	public ModelAndView updateCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return null;
	}
	
	public ModelAndView createCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return null;
	}
	
	private List<CompanyBO> generateCustomerListResult() {
		List<CompanyBO> list = null;
		return list;
	}
	
//	private List<UserBO> getCompanyUsers() throws SureonException {
//		return userMgr.getUserByCompanyID(AppContext.getCompanyID(), 0, 1000);
//	}

	public CompanyMgr getCompanyMgr() {
		return companyMgr;
	}

	public void setCompanyMgr(CompanyMgr companyMgr) {
		this.companyMgr = companyMgr;
	}
}
